
import com.db4ospatial.*;

import com.db4o.Db4o;
import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;

/**
 * Just some mockup code to demonstrate how I imagine usage of db4o-spatial
 * 
 * Integration with db4o looks just like with any other objects. 
 * All the magic (spatial indexing) will be hidden.
 * 
 * All spatial types are wrappers around JTS classes. If you know how to use JTS 
 * then you've nothing new to learn. Otherwise refer to http://www.vividsolutions.com/jts/jtshome.htm
 * There are lots of examples included in JTS source code package. 
 * 
 * @author pablo
 *
 */

public class Mockup
{

	public static void main(String[] args) {
		
		// let's create some geometric objects
		Point point = new Point( new Coordinate4(10, 20) );
		
		Coordinate[] coordinateArray = new Coordinate[] {
				new Coordinate(0, 0), new Coordinate(10, 0), 
				new Coordinate(10, 10), new Coordinate(0, 10)
				};
		LinearRing squareShape = new LinearRing(coordinateArray);
		
		Polygon square = new Polygon(squareShape, null);
		
		LineString line = new LineString(
				new Coordinate[] { new Coordinate(-5, 5), new Coordinate(15, 5) }
			);
		
		
		// open database and store out objects
		ObjectContainer db=Db4o.openFile("spatial.db");
		
		db.set(point);
		db.set(square);
		db.set(line);
		
		db.commit();
		
		
		/** spatial quering */
		
		// assuming there were some objects stored before
		// e.g. borders of countries stored as Polygons, rivers stored as LineStrings 
		// and cities stored as Points
		
		// all cities
		// Query By Example
		ObjectSet<Point> cities = db.get(Point.class);
		for (Point city : cities){
			System.out.println(city);
		}
		
		// all countries with area larger than 300.0
		// Native Query
		ObjectSet<Polygon> largeCountries = db.query(new Predicate<Polygon>() {
			public boolean match(Polygon country) {
				return country.area() > 300.0;
			}
		});
		
		// all countries
		// Query By Example
		final ObjectSet<Polygon> countries = db.get(Polygon.class);
		
		// all rivers longer than 100 which are within some country's border
		// Native Query
		ObjectSet<LineString> rivers = db.query(new Predicate<LineString>() {
			public boolean match(LineString river) { 
				for (Polygon country : countries) {
					return country.contains(river) && river.length() > 100.0;
				}
				return false;
			}
		});
		
		
		db.close();

	}

}
